///<reference path="xbrlViewer/xrbl-viewer.d.ts" />
///<reference path="uploader/uploader.d.ts" />
///<reference path="barrage/barrage.d.ts" />

//1、创建对象,传入iframe元素以及prism样式文件的路径
var xbrl = new XBRLViewer(document.getElementById("frm"), "/xbrlViewer/xbrl-viewer.css");
//2、 加载  两种方式
//xbrl.render(xmlStr)
xbrl.load({ url: "xbrlViewer/demo.xml", method: 'get' }); //参数格式为 asyncRequest函数的ajaxParameters的格式

// var query1 = { //  <unit id="U_RMB"><measure>iso4217:CNY</measure></unit>
//         tagname: 'unit',
//         attrs: { id: 'U_shares' }
//     },
//     contextRef = "C_instant_20150630"
// query2 = { //<cfid-pt:HTSXYLJJFEJZBDJYTQYJBJJZDBDBJJiJinLeiJiFenEJingZhiZengZhangLv contextRef="C_instant_20150630" decimals="INF" unitRef="U_pure">-1.0000</cfid-pt:HTSXYLJJFEJZBDJYTQYJBJJZDBDBJJiJinLeiJiFenEJingZhiZengZhangLv>
//     namespace: 'cfid-pt',
//     tagname: 'HTSXYLJJFEJZBDJYTQYJBJJZDBDBJJiJinLeiJiFenEJingZhiZengZhangLv',
//     attrs: {
//         contextRef: "C_instant_20150630"
//     }
// };
//高亮及修改时，均是操作查询到的第一个结果。尽量保证传入的 query能定位到唯一节点
//3、高亮  success 表示执行结果
// $("#b2").click(function() {
//     var success = xbrl.highLight(query1)
// });
// //4、高亮并修改
// $("#b3").click(function() {
//     var success = xbrl.highLight(query2, "shenjunjian")
// });



new Uploader({
    el: '#up',
    url: "abc.action",
    tmplPath: "/scripts/uploader.html",
    // data: { somedata: "其它需要提交后台的数据" },
    // filter: ["doc", "xls"]
})

var t = new Timer();
new Barrage(t, "hello")
t.start();

//////以下为公司老代码
var globalAjaxRequest = undefined;

function asyncRequest(ajaxParameters, success, context, error, timeout, localXmlHttpRequest) {
    var ajaxRequest = localXmlHttpRequest;
    if (!ajaxRequest) {
        if (!globalAjaxRequest) {
            globalAjaxRequest = new XMLHttpRequest();
        }
        ajaxRequest = globalAjaxRequest;
    }

    if (!ajaxParameters.method) {
        ajaxParameters.method = 'get';
    }

    var stringData = ajaxParameters.data;

    if (ajaxParameters.data) {
        if ('object' === typeof(ajaxParameters.data) && !(ajaxParameters.data instanceof FormData)) {
            stringData = 'data=' + encodeURI(JSON.stringify(ajaxParameters.data), 'UTF-8');
        }

        if ('get' === ajaxParameters.method || 'delete' === ajaxParameters.method) {

            if (ajaxParameters.url.indexOf('?') >= 0) {
                ajaxParameters.url += stringData;
            } else {
                ajaxParameters.url += '?' + stringData;
            }
        }
    }

    ajaxParameters.url = encodeURI(ajaxParameters.url); // 因为每并非所有的浏览器url编码格式都一样,所以这里显式编码
    ajaxRequest.open(ajaxParameters.method, ajaxParameters.url, ('async' in ajaxParameters) ? ajaxParameters.async : true);

    if ('string' === typeof(stringData)) {
        ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
    }

    if (ajaxParameters.responseType) {
        ajaxRequest.responseType = ajaxParameters.responseType;
    } else if (ajaxParameters.async) {
        ajaxRequest.responseType = '';
    }

    function callError(xhr) {
        if (error) {
            error.call(context, xhr);
        }
    }

    ajaxRequest.onload = function() {
        if (200 === this.status || 304 == this.status) {
            if (success) {
                success.call(context, this);
            }
        } else {
            callError(this);
        }

        // this.abort();//reset
    };

    ajaxRequest.ontimeout = function() {
        this.isTimeout = true;
        if (timeout) {
            timeout.call(context, this);
        } else {
            callError(this);
        }
        // this.abort();// reset
    };

    ajaxRequest.onerror = function() {
        callError(this);

        // this.abort();//reset
        alert("与服务器断开,无法完成此操作!");
    };
    ajaxRequest.send(stringData);
}

function importScript(doc, url, callback) {
    var scriptElement = doc.createElement('script');
    scriptElement.type = "text/javascript";

    scriptElement.src = url;
    if ('function' === typeof(callback)) {
        scriptElement.onload = callback;
    }

    doc.head.appendChild(scriptElement);

    return scriptElement;
}


function importCss(doc, url) {
    var cssElement = doc.createElement('link');
    cssElement.setAttribute('rel', 'stylesheet');
    cssElement.setAttribute('type', 'text/css');

    cssElement.setAttribute('href', url);
    doc.head.appendChild(cssElement);

    return cssElement;
}